#!/bin/bash
#用于debian10buster搭建时的简便优化使用。没有具体用途。仅仅优化源、dns解析、安装php环境。
sudo echo -e "[Resolve]\nDNS=1.1.1.1\nDNSOverTLS=yes">/etc/systemd/resolved.conf
systemctl restart systemd-resolved.service
systemd-resolve --status | grep 'DNS Servers'
# 检测是否为Debian 10
if [[ -f /etc/debian_version && $(grep "buster/sid" /etc/debian_version) ]]; then
    echo "Debian 10"

    # 将Debian源设置为默认源
    cat <<EOF > /etc/apt/sources.list
deb http://deb.debian.org/debian buster main contrib non-free
deb-src http://deb.debian.org/debian buster main contrib non-free

deb http://deb.debian.org/debian buster-updates main contrib non-free
deb-src http://deb.debian.org/debian buster-updates main contrib non-free

deb http://deb.debian.org/debian buster-backports main contrib non-free
deb-src http://deb.debian.org/debian buster-backports main contrib non-free

deb http://security.debian.org/debian-security/ buster/updates main contrib non-free
deb-src http://security.debian.org/debian-security/ buster/updates main contrib non-free
EOF

    echo "Debian源已设置为默认源"
# 检测是否为Debian 11
elif [[ -f /etc/debian_version && $(grep "bullseye" /etc/debian_version) ]]; then

    # 将Debian源设置为默认源
    cat <<EOF > /etc/apt/sources.list
deb http://deb.debian.org/debian bullseye main contrib non-free
deb-src http://deb.debian.org/debian bullseye main contrib non-free
deb http://deb.debian.org/debian bullseye-updates main contrib non-free
deb-src http://deb.debian.org/debian bullseye-updates main contrib non-free
deb http://deb.debian.org/debian bullseye-backports main contrib non-free
deb-src http://deb.debian.org/debian bullseye-backports main contrib non-free
deb http://security.debian.org/debian-security/ bullseye-security main contrib non-free
deb-src http://security.debian.org/debian-security/ bullseye-security main contrib non-free
    echo "Debian 11"
# 检测是否为Ubuntu 20.04
elif [[ -f /etc/os-release && $(grep "Focal Fossa" /etc/os-release) ]]; then

    # 将Debian源设置为默认源

    cat <<EOF > /etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse

deb http://archive.canonical.com/ubuntu focal partner
deb-src http://archive.canonical.com/ubuntu focal partner
EOF


    echo "Ubuntu 20.04"
# 检测是否为Ubuntu 22.04
elif [[ -f /etc/os-release && $(grep "Jammy Jellyfish" /etc/os-release) ]]; then
    cat <<EOF > /etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
deb http://archive.canonical.com/ubuntu/ jammy partner
deb-src http://archive.canonical.com/ubuntu/ jammy partner

EOF




    echo "Ubuntu 22.04"
# 未知操作系统版本
else
    echo "未知操作系统版本"
fi

# 更新源并安装php 7.4及其相关扩展和依赖包
apt-get update
apt-get install -y php7.4 php7.4-fpm php7.4-mysql php7.4-curl php7.4-gd php7.4-mbstring php7.4-xml php7.4-zip

# 启用php-fpm
systemctl enable php7.4-fpm.service

# 配置php7.4-fpm
sed -i 's/^;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.4/fpm/php.ini
sed -i 's/^upload_max_filesize = .*/upload_max_filesize = 50M/' /etc/php/7.4/fpm/php.ini
sed -i 's/^post_max_size = .*/post_max_size = 50M/' /etc/php/7.4/fpm/php.ini

# 重启php7.4-fpm
systemctl restart php7.4-fpm.service

# 安装nginx
apt-get install -y nginx

# 配置nginx
cat <<EOF > /etc/nginx/sites-available/default
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files \$uri \$uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
    
    location /test.php {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
}
EOF

# 重启nginx
systemctl restart nginx.service

# 创建测试php页面
echo "<?php phpinfo();" > /var/www/html/test.php

# 检测操作系统版本
if [[ -f /etc/debian_version && $(grep "buster/sid" /etc/debian_version) ]]; then
    echo "Debian 10"
    codename="buster"
elif [[ -f /etc/debian_version && $(grep "bullseye" /etc/debian_version) ]]; then
    echo "Debian 11"
    codename="bullseye"
else
    echo "不支持的操作系统版本"
    exit 1
fi

# 将Tor仓库添加到APT源列表
sudo bash -c "cat <<EOF > /etc/apt/sources.list.d/tor.list
deb [signed-by=/usr/share/keyrings/tor-archive-keyring.gpg] https://deb.torproject.org/torproject.org $codename main
deb-src [signed-by=/usr/share/keyrings/tor-archive-keyring.gpg] https://deb.torproject.org/torproject.org $codename main
EOF"

# 下载并安装Tor的GPG密钥
wget -O- https://deb.torproject.org/torproject.org/\
A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89.asc \
| gpg --dearmor \
| tee /usr/share/keyrings/tor-archive-keyring.gpg >/dev/null

# 更新源并安装Tor
sudo apt-get update
apt install tor deb.torproject.org-keyring

# 检查obfs4proxy是否已安装
if ! command -v obfs4proxy &>/dev/null; then
    echo "obfs4proxy未安装，请先安装obfs4proxy。"
    exit 1
fi

sync; echo 3 > /proc/sys/vm/drop_caches 
swapoff -a



# 获取obfs4proxy的网桥信息
bridge_info=$(obfs4proxy -version | grep "Bridge" | awk '{print $3}')

# 检查是否有网桥信息
if [[ -z "$bridge_info" ]]; then
    echo "没有配置obfs4代理的网桥。"
else
    echo "obfs4代理的网桥信息："
    echo "$bridge_info"
fi

# 安装基本的开发工具
sudo apt-get install build-essential -y

# 安装C/C++编译器和库
sudo apt-get install gcc g++ make libssl-dev -y

#Boost：一套通用C++库
sudo apt-get install libboost-all-dev -y
#Eigen3：一个C++模板库，用于线性代数、矩阵和向量操作
sudo apt-get install libeigen3-dev -y
sudo apt-get install libeigen3-dev libann-dev
# 安装Java编译器和库
sudo apt-get install default-jdk -y

# 安装Python编译器和库
sudo apt-get install python3-pip python3-dev -y
sudo apt install build-essential cmake git
pip3 install --upgrade pip setuptools wheel
# 禁用不必要的服务和应用
sudo systemctl stop cups.socket
sudo systemctl disable cups.socket
sudo systemctl stop avahi-daemon.socket
sudo systemctl disable avahi-daemon.socket
sudo systemctl stop openbsd-inetd.service
sudo systemctl disable openbsd-inetd.service
sudo systemctl stop postfix.service
sudo systemctl disable postfix.service
Git：用于版本控制
apt install git openssl -y

# 安装和配置安全的浏览器
#sudo apt-get install firefox-esr -y
#sudo ln -s /usr/lib/firefox-esr/firefox-esr /usr/bin/firefox
